package com.hd.crdao.adapter;

import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hd.crdao.R;
import com.hd.crdao.base.MyBaseAdapter;
import com.hd.crdao.bean.BrandInfoBean;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.PinyinUtils;
import com.hd.crdao.utils.UIUtils;

import java.util.List;

import static com.hd.crdao.utils.UIUtils.getResources;

/**
 *
 *@author Administrator
 *@date 2017/5/23 0023
 */
public class BrandAdapater extends MyBaseAdapter<BrandInfoBean.RowsBean> {

    public BrandAdapater(List<BrandInfoBean.RowsBean> dataSets) {
        super(dataSets);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
        BrandInfoBean.RowsBean rowsBean = mDataSets.get(position);
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.item, null);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.catalog = (TextView) convertView.findViewById(R.id.catalog);
            viewHolder.ivLog = (ImageView) convertView.findViewById(R.id.iv_logo);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //根据position获取首字母作为目录catalog
        String catalog = rowsBean.getVcFirst();

        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if(position == getPositionForSection(catalog)){
            viewHolder.catalog.setVisibility(View.VISIBLE);
            viewHolder.catalog.setText(rowsBean.getVcFirst());
        }else{
            viewHolder.catalog.setVisibility(View.GONE);
        }
        String vcBrand = rowsBean.getVcBrand();
        viewHolder.name.setText(vcBrand);
        String pingYin = PinyinUtils.getPingYin(vcBrand).toLowerCase().replace(" ","").replace("·","").trim();
        ALog.d(pingYin + "----------------------------------");
        int resID = getResources().getIdentifier(pingYin, "drawable", "com.hd.crdao");
        if (resID == 0) {   //没有这张资源文件
            Drawable img = getResources().getDrawable(R.drawable.about_1_1_3x);
            viewHolder.ivLog.setImageDrawable(img);
        } else {
            Drawable img = getResources().getDrawable(resID);
            viewHolder.ivLog.setImageDrawable(img);
        }
        return convertView;

    }

    final static class ViewHolder {
        ImageView ivLog;
        TextView catalog;
        TextView name;
    }
    /**
     * 获取catalog首次出现位置
     */
    public int getPositionForSection(String catalog) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = mDataSets.get(i).getVcFirst();
            if (catalog.equalsIgnoreCase(sortStr)) {
                return i;
            }
        }
        return -1;
    }
}
